{include file="public/header" /}
<body>
<div class="x-body layui-anim">
    <form class="layui-form" id="form">
        <div class="layui-form-item">
            <label for="start_time" class="layui-form-label">
                <span class="x-red">*</span>上次分红日
            </label>
            <div class="layui-input-inline">
                <input type="text" name="start_time" id="start_time" lay-verify="start_time" placeholder="" autocomplete="off" class="layui-input" value="{if $start_time}{$start_time|date='Y-m-d H:i:s',###}{/if}" readonly="">
            </div>
            <label for="end_time" class="layui-form-label">
                <span class="x-red">*</span>本次分红日
            </label>
            <div class="layui-input-inline">
                <input type="text" name="end_time" id="end_time" lay-verify="end_time" placeholder="" autocomplete="off" class="layui-input" value="{:date('Y-m-d H:i:s')}">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="total_amount" class="layui-form-label">
                <span class="x-red">*</span>期间总收入
            </label>
            <div class="layui-input-inline">
                <input type="text" name="total_amount" id="total_amount" ay-verify="required" autocomplete="off" value="" class="layui-input" readonly="">
            </div>
            <label for="allocation_amount" class="layui-form-label">
                <span class="x-red">*</span>分配金额
            </label>
            <div class="layui-input-inline">
                <input type="text" name="allocation_amount" id="allocation_amount" ay-verify="required" autocomplete="off" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="member_count" class="layui-form-label">
                <span class="x-red">*</span>会员数量
            </label>
            <div class="layui-input-inline">
                <input type="text" id="member_count" name="member_count" lay-verify="required" autocomplete="off" class="layui-input" readonly="">
            </div>
            <!-- <label for="PerAmount" class="layui-form-label">
              <span class="x-red">*</span>平均分配额
            </label>
            <div class="layui-input-inline">
              <input type="text" id="PerAmount" name="PerAmount" lay-verify="required" autocomplete="off" class="layui-input" readonly="">
            </div> -->
        </div>
        <!-- <div class="layui-form-item">
          <label for="ShouldAmount" class="layui-form-label">
            <span class="x-red">*</span>应发金额
          </label>
          <div class="layui-input-inline">
           <input type="text" id="ShouldAmount" name="ShouldAmount" lay-verify="required" autocomplete="off" class="layui-input" readonly="">
          </div>
        </div> -->
        <hr>
        <div class="layui-form-item" style="text-align: center;">
            <a class="layui-btn" onclick="tongji()" href="javascript:;">统计业绩</a>
            <button class="layui-btn" lay-filter="add" lay-submit="">确认发放</button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layedit', 'layer', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#start_time'
            , type: 'datetime',
            trigger: 'click'
        });

        laydate.render({
            elem: '#end_time'
            , type: 'datetime'
            , min: $("#start_time").val()
            , max: $("#end_time").val(),
            trigger: 'click'
        });

        //监听提交
        form.on('submit(add)', function (data) {

            layer.confirm('确定要发放吗？', function (index) {
                layer.close(index);
                var load = layer.load(2);
                $.ajax({
                    type: 'POST',
                    url: '{:url("account/dividends")}',
                    data: $("#form").serialize(),
                    dataType: "json",
                    success: function (data) {
                        layer.close(load);
                        if (data.code == 1) {
                            layer.alert(data.msg, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.window.location.reload();
                            });
                        } else {
                            layer.alert(data.msg);
                        }
                    }
                });
            });
            return false;
        });


    });


    function tongji() {
        var load = layer.load(2);
        var end_time = $("#end_time").val()
        //发异步删除数据
        $.get("{:URL('account/statistics')}?end_time=" + end_time, function (data) {
            layer.close(load);
            $("#should_amount").val(data.should_amount);
            $("#per_amount").val(data.per_amount);
            $("#member_count").val(data.member_count);
            $("#allocation_amount").val(data.allocation_amount);
            $("#total_amount").val(data.total_amount);
        });
    }

    $("#allocation_amount").change(function () {
        var allocation_amount = $("#allocation_amount").val();
        var member_count = $("#member_count").val();
        var per_amount = allocation_amount / member_count;
        $("#per_amount").val(per_amount);
    });

</script>
</body>
</html>